<?php
namespace app\admin\controller;

class Order extends Base{
    //
    protected function initialize() {
        parent::initialize();
        $this->model = 'Order';
    }
    //
    public function blockIp(){
        $ip = $this->request->param('ip', '');
        if(!$ip){
            exit;
        }
        $model = model('BlockOrderIp');
        $row = $model->where('ip', '=', $ip)->find();
        if($row){
            $msg = $this->response('此IP已经在黑名单中.!', false, '', 200);
            return json($msg);
        }
        $data = ['ip'=>$ip, 'addtime'=>time()];
        if($model->insert($data)){
            $msg = $this->response('已将此IP加入黑名单!', false, '', 200);
            return json($msg);
        }
        $msg = $this->response('加入失败，请稍后重试!', false, '', 300);
        return json($msg);
    }
    //
    protected function _before_index(){
        $thead = [
            'oid' => 'OID',
            'xname' => '推广名称',
            'product' => '商品名称',
            'cname' => '客户姓名',
            'telno' => '手机号码',
            'address' => '收货地址',
            'qq' => 'QQ/微信',
            'desc' => '客户留言',
            'payment' => '支付方式',
            'addtime' => '下单时间',
            'mid' => '推广员工',
            'link' => '来路信息',
            'ip' => '客户IP',
            'agent' => '',
        ];
        $this->assign('thead', $thead);
        $condition = [];
        $condition['used'] = 1;
        $memArr = model('Member')->where($condition)->field('mid,nickname')->select()->toArray();
        $this->assign('memArr', $memArr);
        $slow = $this->request->param('slow/d', 0);
        if($slow === 1){
            usleep(300000);
        }
    }
    //
    protected function _filter($map){
        $has_cname = $has_mid = false;
        foreach($map as $key => $row){
            if($row[0] == 'addtime'){
                $date = str_replace('%', '', $row[2]);
                $start = strtotime($date.' 00:00:00');
                $end = strtotime($date.' 23:59:59');
                $map[$key] = ['addtime', ['>=', $start], ['<=', $end], 'and'];
            }elseif($row[0] == 'cname'){
                $has_cname = true;
            }elseif($row[0] == 'mid'){
                $has_mid = true;
            }
        }
        $filter = $this->request->post('keys.filter', 0);
        if(!$has_cname && $filter == 1){
            $varstr = model('GlobalSet')->where('key', '=', 'order_white_cname')->value('value');
            $varArr = $varstr ? explode('|', $varstr) : [];
            if($varArr){
                $map[] = ['cname', 'notin', $varArr];
            }
        }
        $map = array_values($map);
        return $map;
    }
    //
    protected function _after_list($list){
        $midArr = [];
        foreach($list as $row){
            $midArr[] = $row['mid'];
        }
        $mArr = model('Member')->where('mid', 'in', $midArr)->column('nickname', 'mid');
        $payArr = [1=>'货到付款', 2=>'支付宝', 3=>'微信支付'];
        foreach($list as $key => $row){
            $list[$key]['mid'] = $mArr[$row['mid']] ?? '';
            $list[$key]['payment'] = $payArr[$row['payment']] ?? '';
            $list[$key]['addtime'] = date('Y-m-d H:i:s', $row['addtime']);
            $list[$key]['telno'] = authcheck('Admin/Order/telno') ? $row['telno'] : mb_substr($row['telno'], 0, 3).'****'.mb_substr($row['telno'], 7, 4);
            $list[$key]['qq'] = authcheck('Admin/Order/qq') ? $row['qq'] : mb_substr($row['qq'], 0, 3).'***';
            $list[$key]['link'] = strlen($row['link']) > 50 ? mb_substr($row['link'], 0, 50).'...[<a href="'.$row['link'].'" target="_blank" title="'.$row['link'].'">more</a>]' : $row['link'];
            $list[$key]['agent'] = ' <i agent="'.$row['agent'].'" class="fa fa-'.agent($row['agent']).'"></i> ';
        }
        return $list;
    }
    //
    protected function _filter_del(){
        return [];
    }
    //
    protected function _after_del($rows){
        $logs = [];
        foreach($rows as $row){
            $logs[] = ['pk' => 'oid', 'value' => $row['oid'], 'row' => $row];
        }
        $this->logAll($logs);
    }
}